<template>
  <div class="login-container">
    <div class="login-form">
      <img
        src="@/assets/loginBC/logo.595745bd.png"
        alt=""
        class="title-container"
      >
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        autocomplete="on"
        label-position="left"
      >
        <el-form-item prop="loginName">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="loginName"
            v-model="loginForm.loginName"
            placeholder="请输入用户名"
            name="loginName"
            maxlength="18"
            type="text"
            tabindex="1"
            autocomplete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="请输入密码"
            maxlength="20"
            name="password"
            tabindex="2"
            autocomplete="on"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon
              :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"
            />
          </span>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="17">
              <span class="svg-container">
                <i class="el-icon-edit" />
              </span>
              <el-input
                ref="code"
                v-model="loginForm.code"
                placeholder="请输入验证码"
                name="code"
                type="text"
                maxlength="4"
                @keyup.enter.native="handleLogin"
              /></el-col>
            <el-col :span="6">
              <div class="clientToken" @click="clientToken">
                <img :src="codeImg" alt=""></div></el-col>
          </el-row>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          style="width: 100%; margin-bottom: 30px"
          @click.native.prevent="handleLogin"
        >Login</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
import { clientToken } from '@/api/user'
import { mapActions } from 'vuex'
export default {
  name: 'Login',
  data() {
    return {
      codeImg: '',
      random: Math.ceil(Math.random() * 10),
      loginForm: {
        loginName: 'admin',
        password: 'admin',
        clientToken: null,
        code: null,
        loginType: 0
      },
      loginRules: {
        loginName: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'blur'
          }
        ]
      },
      passwordType: 'password',
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  async created() {
    this.clientTokenApi()
  },
  mounted() {
    if (this.loginForm.loginName === '') {
      this.$refs.loginName.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    ...mapActions(['user/fetchLogin']),
    handleLogin() {
      // await login(this.loginForm)
      // console.log('登录成功')
      // this.$router.push('/')
      this.$refs.loginForm.validate(async(valid) => {
        if (valid) {
          try {
            this.loading = true
            await this['user/fetchLogin'](this.loginForm)
            this.$router.push('/')
          } catch (error) {
            console.log(error)
          } finally {
            this.loading = false
          }
        }
      })
    },
    // 验证码接口数据
    async clientTokenApi() {
      const res = await clientToken(this.random)
      this.loginForm.clientToken = this.random
      if (res.headers['content-type'].indexOf('json') !== -1) {
        this.tips('验证码获取次数过多，请稍后重试', 'warning')
      } else {
        this.codeImg =
          'data:image/png;base64,' +
          btoa(
            new Uint8Array(res.data).reduce(
              (data, byte) => data + String.fromCharCode(byte),
              ''
            )
          )
      }
    },
    clientToken() {
      // 点击验证码随机生成
      this.clientTokenApi()
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: #283443;
$light_gray: #fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  .el-form-item__content {
    background-color: #fff;
  }
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: #999999;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  background-image: url("~@/assets/loginBC/background.be4fae7d.png");
  background-position: center; //将图片位置设置为充满整个屏幕
  overflow: hidden;
  position: relative;
  .login-form {
    width: 518px;
    height: 388px;
    background-color: #fff;
    max-width: 100%;
    padding: 76px 35px 0;
    border-radius: 10px;
    position: absolute;
    top: 50%;
    left: 50%;
    margin-top: -194px;
    margin-left: -259px;
  }
  .clientToken {
    width: 130px;
    height: 50px;
    background-color: pink;
  }
  .el-button--primary  {
    background: linear-gradient(262deg, #2e50e1, #6878f0);
    height: 52px;
    border-radius: 8px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: absolute;
    top: -46px;
    left: 50%;
    margin-left: -46px;
    width: 96px;
    height: 96px;
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

  @media only screen and (max-width: 470px) {
    .thirdparty-button {
      display: none;
    }
  }
}
</style>
